package es.mgil.ejercicios.clase;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class DibujarAvanzado extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(new RenderView(this));
	}

	private class RenderView extends View {
		private Paint paint;
		Point point;

		// circle
		private boolean growCircle;
		private int minRadius;
		private int maxRadius;
		private int currentRadius;

		// rectangle
		private Rect rect;
		private boolean growRectangle;
		private int minX;
		private int maxX;
		private int currentX;

		// line
		private int line_end_x;
		private int line_end_y;
		private boolean upLine;

		public RenderView(Context context) {
			super(context);
			paint = new Paint();
			point = new Point();
			getWindowManager().getDefaultDisplay().getSize(point);

			// circle
			minRadius = 0;
			maxRadius = point.x / 2;
			currentRadius = minRadius;

			// rectangle
			rect = new Rect();
			minX = 0;
			maxX = point.x / 2;
			currentX = maxX;

			// line
			line_end_x = point.x - 1;
			line_end_y = 0;
			upLine = false;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// draw the background color
			canvas.drawRGB(255, 255, 255);

			{
				// draw a circle
				if (currentRadius == minRadius) {
					growCircle = true;
				} else if (currentRadius == maxRadius) {
					growCircle = false;
				}

				paint.setStyle(Style.FILL_AND_STROKE);
				paint.setColor(Color.RED);
				paint.setAlpha(0xaa);
				canvas.drawCircle(canvas.getWidth() / 2,
						canvas.getHeight() / 2, currentRadius, paint);

				if (growCircle) {
					currentRadius++;
				} else {
					currentRadius--;
				}
			}

			{
				// draw a rectangle
				if (currentX == minX) {
					growRectangle = false;
				} else if (currentX == maxX) {
					growRectangle = true;
				}

				paint.setStyle(Style.FILL);
				paint.setColor(Color.GREEN);
				paint.setAlpha(0xcc);
				rect.left = currentX;
				rect.top = (point.y / 2) - (maxX - currentX);
				rect.right = maxX * 2 - currentX;
				rect.bottom = (point.y / 2) + maxX - currentX;
				canvas.drawRect(rect, paint);

				if (growRectangle) {
					currentX--;
				} else {
					currentX++;
				}
			}

			{
				// draw a movable line
				if (line_end_x == canvas.getWidth() - 1) {
					if (upLine) {
						line_end_y--;
					} else {
						line_end_y++;
					}
				}

				if (line_end_y == canvas.getHeight() - 1) {
					if (upLine) {
						line_end_x++;
					} else {
						line_end_x--;
					}
				}

				if (line_end_x == 0 && line_end_y == canvas.getHeight() - 1) {
					upLine = true;
				} else if (line_end_x == canvas.getWidth() - 1
						&& line_end_y == 0) {
					upLine = false;
				}

				paint.setColor(Color.BLUE);
				canvas.drawLine(0, 0, line_end_x, line_end_y, paint);
			}

			invalidate();
		}
	}
}
